package com.sgzn.sgpay.service.impl;

import com.sgzn.sgpay.enums.DeviceType;
import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.model.request.SgsDeviceQueryAddPreInfoRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.vo.DeviceQueryAddPreInfoVo;
import com.sgzn.sgpay.model.response.vo.DeviceTypeVo;
import com.sgzn.sgpay.model.response.vo.ShopVo;
import com.sgzn.sgpay.persistence.domain.TDevice;
import com.sgzn.sgpay.persistence.domain.TShop;
import com.sgzn.sgpay.persistence.manager.DeviceDaoManager;
import com.sgzn.sgpay.persistence.manager.ShopDaoManager;
import com.sgzn.sgpay.service.SgsDeviceQueryAddPreInfoService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import com.sgzn.sgpay.util.ArgsUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 设备新增前查询所需信息服务接口实现类
 * @author huangsenguang
 * @version Id: SgsDeviceQueryAddPreInfoServiceImpl.java, v 0.1 2018/5/19 18:12 huangsenguang Exp $$
 */
@Slf4j
@Service
public class SgsDeviceQueryAddPreInfoServiceImpl implements SgsDeviceQueryAddPreInfoService {

    @Resource
    private ShopDaoManager shopDaoManager;

    @Resource
    private DeviceDaoManager deviceDaoManager;

    /**
     * 最大场地编号
     */
    private static int MAX_SITE_NUM = 16;

    @Override
    public SgsResultResponse<DeviceQueryAddPreInfoVo> queryAddPreInfo(SgsDeviceQueryAddPreInfoRequest request) {
        log.info("merchantId:{}", request.getInitiationID());
        //根据商户ID查询所有门店
        List<TShop> shoplist = shopDaoManager.getByMch(request.getMchId());
        ArgsUtils.notEmpty(shoplist, SgsResponseCode.C_SHOP_NOT_FOUND.getResponseCode(), SgsResponseCode.C_SHOP_NOT_FOUND.getResponseDesc());
        List<ShopVo> shopVoList = this.getShopVoList(shoplist);//门店下拉列表集合

        List<DeviceTypeVo> deviceTypeList = this.getDeviceTypeMap();//设备下拉列表集合

        Map<String, List<String>> siteNumMap = this.getSiteNumMap(shoplist,request.getDeviceId());//可选场地编号下拉列表集合

        return SgsResponseUtil.INSTANCE.success(assembleResponse(shopVoList,deviceTypeList,siteNumMap));
    }

    /**
     * 组装最终响应结果
     * @param shopVoList
     * @param deviceTypeList
     * @param siteNumMap
     * @return
     */
    private DeviceQueryAddPreInfoVo assembleResponse(List<ShopVo> shopVoList,List<DeviceTypeVo> deviceTypeList,
                                                     Map<String, List<String>> siteNumMap) {
        DeviceQueryAddPreInfoVo result = new DeviceQueryAddPreInfoVo();
        result.setShopVoList(shopVoList);
        result.setDeviceTypeList(deviceTypeList);
        result.setSiteNumMap(siteNumMap);
        return result;
    }

    /**
     * 获取可选场地编号集合
     * @param shoplist 未去重的门店列表
     * @param defDeviceId 无需排除的设备唯一码
     * @return
     */
    private Map<String, List<String>> getSiteNumMap(List<TShop> shoplist,String defDeviceId){
        //获取门店ID列表
        List<String> shopIdList = this.getShopIdList(shoplist);
        //获取原始设备列表
        List<TDevice> deviceList = this.deviceDaoManager.getAllDevices(shopIdList);
        Map<String, List<String>> siteNumMap = new HashMap<>(shopIdList.size());
        //初始化map
        for (String shopId : shopIdList) {
            List<String> tempList = new ArrayList<>(MAX_SITE_NUM);
            for (int index = 1; index <= MAX_SITE_NUM; index++) {
                tempList.add(String.valueOf(index));
            }
            siteNumMap.put(shopId, tempList);
        }
        //去除已有场地编号
        for(TDevice item : deviceList){
            if(StringUtils.isNotBlank(defDeviceId) && defDeviceId.equals(item.getDeviceId())){
                //如果传入的deviceId非空且等于当前迭代的设备编码，跳过本次迭代【即自身的场地编码作为可选的场地编码】
                continue;
            }
            String siteNum = item.getSiteNum();
            List<String> strings = siteNumMap.get(item.getShopId());
            if (!CollectionUtils.isEmpty(strings)) {
                strings.remove(siteNum);
            }
        }
        return siteNumMap;
    }

    /**
     * 获取设备类型
     * @return
     */
    private List<DeviceTypeVo> getDeviceTypeMap() {
        List<DeviceTypeVo> list = new ArrayList<>(2);
        for(DeviceType single : DeviceType.values()){
            DeviceTypeVo deviceTypeVo = new DeviceTypeVo();
            deviceTypeVo.setCode(single.getCode());
            deviceTypeVo.setName(single.getDesc());
            list.add(deviceTypeVo);
        }
        return list;
    }

    /**
     * 获取下拉门店列表
     * @param shopList
     * @return
     */
    private List<ShopVo> getShopVoList(List<TShop> shopList) {
        List<ShopVo> voList = new ArrayList<>();
        for (TShop tShop : shopList) {
            ShopVo shopVo = new ShopVo();
            shopVo.setShopId(tShop.getShopId());
            shopVo.setShopName(tShop.getShopName());
            voList.add(shopVo);
        }
        log.info("List<ShopVo>:{}", voList.toString());
        return voList;
    }

    /**
     * 获取门店id 集合
     * @param shops
     * @return
     */
    private List<String> getShopIdList(List<TShop> shops) {
        HashSet<String> set = new HashSet<>(shops.size());
        for (TShop shop : shops) {
            set.add(shop.getShopId());
        }
        List<String> shopIdList = new ArrayList<>(shops.size());
        shopIdList.addAll(set);
        return shopIdList;
    }
}

